<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .v_code .code {
            display: inline-block;
            width: 100px;
            height: 30px;
            background-color: pink;
            border: 1px solid #ccc;
            font-size: 28px;
            line-height: 30px;
            text-align: center;
            color: darkturquoise;
        }
    </style>
</head>

<body>
    <div class="v_code">
        <div class="code_show">
            <span class="code" id="checkCode"></span>
            <a id="linkbt">看不清换一张</a>
        </div>
        <div class="input_code">
            <label for="inputCode">验证码：</label>
            <input type="text" id="inputCode" />
            <span id="text_show"></span>
        </div>
        <input id="Button1" type="button" value="确定" />

    </div>
    <script>
        window.onload = function() {
            var res = getCode();

            function getCode() {
                var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
                var str = '';
                for (var i = 0; i < 6; i++) {
                    var num = Math.round(Math.random() * (15 - 0) + 0);
                    str += arr[num];
                }
                return str;

            }
            document.getElementById('checkCode').innnerText = res;
            document.getElementById('linkbt').onclick = function() {
                document.getElementById('checkCode').innerText = getCode();
            }

            document.getElementById('Button1').onclick = function() {
                var code = document.getElementById('checkCode').innnerText;
                var inputCode = document.getElementById('inputCode').value;
                if (code != inputCode) {
                    alert('您输入的验证码不正确');
                    document.getElementById('inputCode').value = '';
                    return false;

                }

            }

        }
    </script>
</body>

</html>